#  Public and for release, CBEC v3.0.01 Build 1985+ [17th May, 2023]
#
#  Copyright(c) Ivyware Pty Ltd 2018-23  (all rights reserved)
#               MELBOURNE, VICTORIA, AUSTRALIA, 3000
#
#  This file is provided as-is by Ivyware Pty Ltd.  No claims are made
#  as to fitness for any particular purpose.  No warranties of any kind
#  are expressed or implied.  The recipient agrees to determine
#  applicability of information provided.
#
#  Ivyware hereby grants the right to freely use the information
#  supplied in this file for the creation of Python Automation scripts
#  supporting the Chartboard Application, and to make copies of this
#  file in any form for internal or external distribution as long as
#  this notice remains attached.
#
#  No waranty or suitability for purpose is implied.
#
#  Simple Python Advisor script that uses the Aroon indicator to generate
#  buy and sell signals
#  NOTES: Above charts need to be active in chart stack for script to
#         proceed through to completion
#       : Requirement is for phython 3.8 to be installed
#       : Based upon Chartboard Extension Classes (CBEC) shipped with
#         Chartboard product
#    ***: Script under development and subject to change without notice***
#

import sys
sys.path.insert(0, 'C:\\Program Files\\Chartboard\\PythonScripts') # CBEC folder
from PythonCBEC import *
import ctypes  # An included library with Python install.   
from datetime import datetime

#
#  Environment variables
#  NOTES: List of current environment variables suitable for debugging
import os
print ( 'Python environment')
for param in os.environ.keys():
    print ( "%20s %s" % (param,os.environ[param]) )

#
#   Establish Root of which all other objects are descendants
#   NOTES: Effectively the Chartboard application itself
oCRoot = CRoot

#
#   Establish CStack object
#   NOTES: Effectively the View tab under which this python script is running
#              and identified by the 'this' tag
#            : Stack type MUST identify as 'OHLCvs'
oCStack = oCRoot.CStackFactory('this');
print ( 'CStack Period Units:' + oCStack.sPUnits)
print ( 'CStack Stock Code:' + oCStack.StockCode())
print ( oCStack)
print ( 'CStack Operative Time=' + str(oCStack.dDATE) )
print ( 'CStack Period Units=' + str(oCStack.nPUnits) )
#
#   Aroon Chart test
#   NOTES: Logically Aroon chart needs to exist within the chart stack
#            : Otherwise create temporary instance of chart for life of Advisor
oCStack.Prerequisites('Aroon')
if oCStack.ChartExists('Aroon'):
    oChartAroon = oCStack.ChartFactory('Aroon')
    #
    # Testing DSeriesAroon
    oDSeriesAroon = oChartAroon.DSeriesFactory('Aroon')
    print('Aroonperiods='+str(oDSeriesAroon.iAroonperiods))
    print('AroonHi='+str(oDSeriesAroon.GetValue_d("AroonHi",0,0)))
    print('AroonLo='+str(oDSeriesAroon.GetValue_d("AroonLo",0,0)))

#################################################
#   Process DSeries bars for monthly, weekly and daily period units
#   NOTES: Evaluates buy shade bars based on monthly period data
#          when a AroonUp() > 50 and AroonDown() < 50
#        : Evaluates sell shade bars based on monthly period data
#          when a AroonUp() < 50 and AroonDown() > 50
#        : Referencing CStack applies settings to all Charts, individual
#          Charts can be referenced directly
oCStack.PYCB_ShadeBarClear(0);
oCStack.PYCB_ShadeBarMask(0,(1<<SBTYPE_Bullish)|(1<<SBTYPE_Bearish))
#
#   Monthly buy signals
#   NOTES: Evaluates both buy and sell shade bars using Aroon relativity
if oCStack.ChartExists('Aroon'):
    oCStack.Rewind()
    while oCStack.Step(PUNITS_Month,1):
        dAroonHi = oDSeriesAroon.GetValue_d('AroonHi',PUNITS_Month,0)
        dAroonLo = oDSeriesAroon.GetValue_d('AroonLo',PUNITS_Month,0)
        if ( dAroonHi is not None and dAroonHi > 60.0 and
             dAroonLo is not None and dAroonLo < 40 ):
            oCStack.PYCB_ShadeBarUpdate(PUNITS_Month,0,SBTYPE_Bullish,11)
        if ( dAroonHi is not None and dAroonHi < 40.0 and
             dAroonLo is not None and dAroonLo > 60.0 ):
            oCStack.PYCB_ShadeBarUpdate(PUNITS_Month,0,SBTYPE_Bearish,11)
#
#   Weekly buy signals
#   NOTES: Evaluates both buy and sell shade bars using Aroon relativity
if oCStack.ChartExists('Aroon'):
    oCStack.Rewind()
    while oCStack.Step(PUNITS_Week,1):
        dAroonHi = oDSeriesAroon.GetValue_d('AroonHi',PUNITS_Week,0)
        dAroonLo = oDSeriesAroon.GetValue_d('AroonLo',PUNITS_Week,0)
        if ( dAroonHi is not None and dAroonHi > 60.0 and
             dAroonLo is not None and dAroonLo < 40 ):
            oCStack.PYCB_ShadeBarUpdate(PUNITS_Week,0,SBTYPE_Bullish,11)
        if ( dAroonHi is not None and dAroonHi < 40.0 and
             dAroonLo is not None and dAroonLo > 60.0 ):
            oCStack.PYCB_ShadeBarUpdate(PUNITS_Week,0,SBTYPE_Bearish,11)
#
#   Daily buy signals
#   NOTES: Evaluates both buy and sell shade bars using Aroon relativity
if oCStack.ChartExists('Aroon'):
    oCStack.Rewind()
    while oCStack.Step(PUNITS_Day,1):
        dAroonHi = oDSeriesAroon.GetValue_d('AroonHi',PUNITS_Day,0)
        dAroonLo = oDSeriesAroon.GetValue_d('AroonLo',PUNITS_Day,0)
        if ( dAroonHi is not None and dAroonHi > 60.0 and
             dAroonLo is not None and dAroonLo < 40 ):
            oCStack.PYCB_ShadeBarUpdate(PUNITS_Day,0,SBTYPE_Bullish,11)
        if ( dAroonHi is not None and dAroonHi < 40.0 and
             dAroonLo is not None and dAroonLo > 60.0 ):
            oCStack.PYCB_ShadeBarUpdate(PUNITS_Day,0,SBTYPE_Bearish,11)



